/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.cells;

import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextArea;
import org.apache.commons.lang3.StringUtils;

/**
 * 表可复制内容列
 * @param <T>
 * @author zpy
 * @version 1.0.0
 */
public class CopyTableCell<T> extends TableCell<T,String> {
    private final static String COPY_CONTROL_STYLE = "copy-text-control";

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        this.setText(null);
        this.setGraphic(null);
        if(StringUtils.isNotBlank(item)){
            TextArea node = new TextArea();
            node.setText(enterProcess(item));
            node.setWrapText(true);
            node.setEditable(false);
            node.getStyleClass().add(COPY_CONTROL_STYLE);
            this.setAlignment(Pos.CENTER_LEFT);
            this.setGraphic(node);
            this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        }
    }

    private String enterProcess(String value){
        if(StringUtils.contains(value,"\\n")){
            return StringUtils.replace(value,"\\n","\n");
        }else{
            return value;
        }
    }
}